/*
 * @Author: jhw
 * @Date: 2025-08-08 10:48:37
 * @LastEditors: jhw
 * @LastEditTime: 2025-08-22 14:44:54
 * @Description:
 */
import { isNoteDesc, type Status } from '@/types';
import { computed, ref } from 'vue';

// 计算组件编号
export const useSerialNum = (coms: Status[]) => {
  return computed(() => {
    let serialNum = 1;
    return coms.map((com) => {
      if (!isNoteDesc(com.name)) {
        return serialNum++;
      }
      return null;
    });
  });
};

// 设置时间相关状态
export const useTime = () => {
  const startTime = ref<Date | null>(null);
  const endTime = ref<Date | null>(null);
  const totalDuration = ref<number>(0); // 总耗时（毫秒）
  const isAnswering = ref<boolean>(false);

  // 开始答题
  const startAnswering = () => {
    if (!startTime.value) {
      startTime.value = new Date();
      isAnswering.value = true;
      console.log('开始答题时间：', startTime.value);
    }
  };

  // 结束答题
  const endAnswering = () => {
    if (startTime.value && !endTime.value) {
      endTime.value = new Date();
      isAnswering.value = false;
      totalDuration.value = endTime.value.getTime() - startTime.value.getTime();
      console.log('结束答题时间：', endTime.value);
      console.log('总耗时（毫秒）：', totalDuration.value);
      console.log('总耗时（分钟）：', Math.round((totalDuration.value / 60000) * 100) / 100);
    }
  };

  return {
    startTime,
    endTime,
    totalDuration,
    isAnswering,
    startAnswering,
    endAnswering,
  };
};
